package com.encapi.framework.config;

import com.encapi.framework.handler.CryptHandler;
import com.encapi.framework.handler.impl.AesCryptHandler;
import com.encapi.framework.handler.impl.RsaAeaCryptHandler;
import com.encapi.framework.handler.impl.RsaCryptHandler;
import com.encapi.framework.properties.AesProperties;
import com.encapi.framework.properties.EncapiProperties;
import com.encapi.framework.properties.RsaAesProperties;
import com.encapi.framework.properties.RsaProperties;
import com.encapi.framework.resolver.DecEntityHandlerMethodArgumentResolver;
import com.encapi.framework.resolver.DecParamHandlerMethodArgumentResolver;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;

/**
 * 自动装配配置
 * @author xzbcode
 */
@Configuration
public class EncapiAutoConfiguration implements WebMvcConfigurer {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(decParamHandlerMethodArgumentResolver());
        resolvers.add(decEntityHandlerMethodArgumentResolver());
    }

    @Bean
    public HandlerMethodArgumentResolver decParamHandlerMethodArgumentResolver() {
        return new DecParamHandlerMethodArgumentResolver();
    }

    @Bean
    public HandlerMethodArgumentResolver decEntityHandlerMethodArgumentResolver() {
        return new DecEntityHandlerMethodArgumentResolver();
    }

    @Bean
    public EncapiProperties encapiProperties() {
        return new EncapiProperties();
    }

    @Bean
    @ConditionalOnProperty(prefix = "encapi.", name = "alg", havingValue = "aes")
    public AesProperties aesProperties() {
        return new AesProperties();
    }

    @Bean
    @ConditionalOnProperty(prefix = "encapi.", name = "alg", havingValue = "rsa")
    public RsaProperties rsaProperties() {
        return new RsaProperties();
    }

    @Bean
    @ConditionalOnProperty(prefix = "encapi.", name = "alg", havingValue = "rsa-aes")
    public RsaAesProperties rsaAesProperties() {
        return new RsaAesProperties();
    }

    @Bean
    @ConditionalOnBean(AesProperties.class)
    public CryptHandler aesCryptHandler() {
        return new AesCryptHandler();
    }

    @Bean
    @ConditionalOnBean(RsaProperties.class)
    public CryptHandler rsaCryptHandler() {
        return new RsaCryptHandler();
    }

    @Bean
    @ConditionalOnBean(RsaAesProperties.class)
    public CryptHandler rsaAeaCryptHandler() {
        return new RsaAeaCryptHandler();
    }

}
